package Assesment
{
	import Assets.Controls.containerStyle;
	
	import SplineComponent.SplineComponentModel;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import flash.xml.XMLNode;
	
	import mx.controls.Alert;
	
	public class AssesmentModel extends SplineComponentModel
	{		
			private var container_obj:containerStyle=new containerStyle;
			private var myWriter:simpleFlvWriter = simpleFlvWriter.getInstance();
			private var recordInterval:uint;
			private var curVidFile:File;
			private var view:AssesmentView;
			private var newFileNameStr:String=new String;
			
			public var questionStatementArray:Array=new Array();
			public var answerOptionsArray:Array=new Array;
			public var correctAnswerArray:Array=new Array;
			public var questionWeightageArray:Array=new Array;
			//XML variables
			
			private var XMLData:XML;
			private var XMLNodes:XMLNode;
			
			private var lessonModel_object:Object=new Object;
			private var lessonView_object:Object=new Object;
			private var lessonEditMode_bool:Boolean;
			
			private var controller:AssesmentController;
			
			//private var module:Module;
		public function AssesmentModel(lessonModel:Object,lessonView:Object,lessonPath:String,lessonEditMode:Boolean):void
		{
			super();
			
			this.lessonModel_object = lessonModel;
			this.lessonView_object = lessonView;
			this.lessonEditMode_bool=lessonEditMode;
			
			this.controller = new AssesmentController(this);
			this.view= new AssesmentView(this,this.controller,this.lessonView_object,this.lessonEditMode_bool);
			
			this.addObserver(this.view);			
			
			this.loadXmlFile();
			
		}
		
		public function loadXmlFile():void
		{
			
			var loader:URLLoader=new URLLoader;
			var request:URLRequest=new URLRequest("C:\\check\\assesment.xml");
			
			loader.addEventListener(IOErrorEvent.IO_ERROR,evtListner_handleFileExceptionError);
			
			loader.load(request);
			loader.addEventListener(Event.COMPLETE,evtListner_XmlLoadComplete);
			
		}
		
		public function evtListner_handleFileExceptionError(evt:IOErrorEvent):void
		{
			trace("error");
		}
		
		
		public function evtListner_XmlLoadComplete(event:Event):void
		{
			trace("ankur");
			var Data:XML=new XML(event.target.data);
			trace(Data);
			var	i:int=0;
			for each(var question:XML in Data.question)
			{	
				this.questionStatementArray[i]=question.@questionStatement.toString();
				this.answerOptionsArray.push(question.@options.toString());
				this.correctAnswerArray.push(question.@answer.toString());
				this.questionWeightageArray.push(question.@weightage.toString());
				i++;
			}
			trace("anks"+questionStatementArray);
		}
		
		public function recordVideo():void
		{
				if(view.recordButton.label == "Record")
				{
					
					curVidFile=new File;
					
					curVidFile = File.createTempFile();
					trace("temp file created at: " + curVidFile.nativePath );
					myWriter.createFile(curVidFile, 360,320, 10);
					recordInterval = setInterval(recordVid, 100);
					view.recordButton.label = "Stop";
					
				}
			else
				{
					
					clearInterval(recordInterval);
					myWriter.closeFile();
					newFileNameStr = curVidFile.name.split(".tmp").join(".flv");
					trace("newFileName: " + newFileNameStr);
					
					var desFile:File = new File(File.desktopDirectory.nativePath +"\\" + newFileNameStr);
					curVidFile.copyTo(desFile);
					view.recordButton.label = "Record";
					
				}
		}
		
		private function recordVid():void
			{
				
				trace("recordVid called...");	
				var snapshot:BitmapData = new BitmapData(320, 240, true);
				snapshot.draw(this.view.video);
				myWriter.saveFrame( snapshot );
				
			}
			
			public function recordAudio():void
			{
				Alert.show("please turn on the Audio Utility file");
			}
			
			public function saveMultipleChoiceQuestions():void
			{
		
				questionStatementArray.push(view.questionStatement.text);
				answerOptionsArray.push(view.AOption.text+"|"+view.BOption.text+"|"+view.COption.text+"|"+view.DOption.text);
				
				correctAnswerArray.push(view.askForAnswerText.text);
				questionWeightageArray.push(view.askForWeightage.text);
				trace(questionStatementArray);
			}
			
			public function saveMultipleChoiceQuestionsToXmlFile():void
			{
				var updateFile:File=new File;
				var outStream:FileStream=new FileStream;
				updateFile.nativePath="C:\\check\\assesment.xml";	
				outStream.open(updateFile,FileMode.WRITE);
					
				this.XMLData=
					<Assesment>
					</Assesment>;
					var question:XMLNode;
					for(var i:int=0;i<questionStatementArray.length;i++)
						{
							question=new XMLNode(1,"question");
							question.attributes.questionStatement=questionStatementArray[i];
							question.attributes.options=answerOptionsArray[i];
							question.attributes.answer=correctAnswerArray[i];
							question.attributes.weightage=questionWeightageArray[i];
						
							this.XMLData.appendChild(question);
					
						}
				
				
					
				outStream.writeUTFBytes(XMLData.toXMLString());
				outStream.close();
				
			}
			
			public function showPreviousQuestion():void
			{
				
			}
  //-----------------------------------------communication functions--------------------------------------------//
  			public function incomingMessageGateway(infoObj_object:Object):void
  			{
  				var message:String=infoObj_object.message.toString();
			
				switch(message)
				{
					case "mouseUpEvent":
						trace("AssesmentModel------->event:mouseup  infoObj.event :"+infoObj_object.event);
						this.setChanged();
						this.notifyObservers(infoObj_object);
						break;
				}
  			}
	}
}